﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Script.UI.Component
{
    public class ToggleGroupComp : MonoBehaviour
    {
        private Dictionary<string,ToggleData> toggles = new Dictionary<string, ToggleData>();

        public Action<ToggleData> ToggleCallback;
        public ToggleData current;
        private void Awake()
        {
            var list =gameObject.GetComponentsInChildren<ToggleData>();
            for (var i = 0; i < list.Length; i++)
            {
                toggles.Add(list[i].data, list[i]);
                list[i].callback += OnToggleCallBack; 
            }
        }

        private void OnToggleCallBack(ToggleData data, bool active)
        {
            if (current == data)
            {
                return;
            }

            current = data;
            ToggleCallback?.Invoke(data);
        }

        public void SetChoose(string choose)
        {
            if (current == toggles[choose])
            {
                return;
            }
            toggles[choose].OnValueChanged(true);
        }
    }
}